/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBeans;

import entity.Banking;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import sessionBeans.BankingFacadeLocal;

/**
 *
 * @author Minh Luc
 */
@ManagedBean
@SessionScoped
public class BankingManagement implements Serializable {

    @EJB
    private BankingFacadeLocal bankingFacade;
    private Banking banking = new Banking();
    private Banking selectBanking;
    private List<Banking> list = null;
    private List<Banking> filteredList = null;

    public void create() {
        if (!validate(banking)) {
            return;
        }

        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
        try {
            bankingFacade.create(banking);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Create Banking", "Create Successful");
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Create Banking Err", e.getMessage() + " Duplicate Code");
        }
        banking = new Banking();
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("create", true);
    }

    public void edit() {
        if (!validate(selectBanking)) {
            return;
        }
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;

        try {
            bankingFacade.edit(selectBanking);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Edit Banking", "Edit Successful");
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", e.getMessage());
        }

        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("edit", true);
    }

    public Banking getBanking() {
        return banking;
    }

    public void setBanking(Banking banking) {
        this.banking = banking;
    }

    public List<Banking> getList() {
        list = bankingFacade.findAll();
        return list;
    }

    public List<Banking> getFilteredList() {
        return filteredList;
    }

    public void setFilteredList(List<Banking> filteredList) {
        this.filteredList = filteredList;
    }

    public Banking getSelectBanking() {
        return selectBanking;
    }

    public void setSelectBanking(Banking selectBanking) {
        this.selectBanking = selectBanking;
    }

    //Validate Bank
    private boolean validate(Banking banking) {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;

        if (banking.getBankName() == null || banking.getBankName().length() == 0) { //BankName
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "BankName is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (banking.getBankName().length() < 2 || banking.getBankName().length() > 30) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "BankName is from 2 - 30 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isCharacter(banking.getBankName())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "BankName is only Character");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }

        if (banking.getBankPhone() == null || banking.getBankPhone().length() == 0) {//BankPhone
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "BankPhone is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (!util.Validate.isPhone(banking.getBankPhone())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "BankPhone is from 8 - 14 Numbers ");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }

        if (banking.getAddress() == null || banking.getAddress().length() == 0) {//BankAddress
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "BankAdress is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (banking.getAddress().length() < 2 || banking.getAddress().length() > 300) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "BankAdress is from 2 - 300 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isContent(banking.getAddress())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "BankAdress is only Character");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }

        if (banking.getEmail() == null || banking.getEmail().length() == 0) {//BankEmail
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Email is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (!util.Validate.isValidEmail(banking.getEmail())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Format Email is xxx@xxx.xxx");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }

        if (banking.getBankNumber() == null || banking.getBankNumber().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "BankNumber is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (!util.Validate.isBankNumber(banking.getBankNumber())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "BankNumber is from 12 - 16 Numbers");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }
        return true;
    }
}
